﻿using Information.System.Hostd.Authorization;
using Information.System.Hostd.Extensions;
using Information.System.Interfaces;
using Information.System.Model;
using Information.System.Model.Dto;
using Information.System.Model.Dto.System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Information.System.Hostd.Controllers.System
{
    /// <summary>
    /// 角色权限
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserDepartmentController : BaseController
    {
        /// <summary>
        /// 日志管理接口
        /// </summary>
        private readonly ILogger<UserDepartmentController> _logger;

        /// <summary>
        /// 会话管理接口
        /// </summary>
        private readonly TokenManager _tokenManager;


        /// <summary>
        /// 用户角色接口
        /// </summary>
        private readonly ISysUserDepartmentService _sysUserDepartmentService;

        /// <summary>
        /// 用户接口
        /// </summary>
        private readonly ISysUsersService _userService;

        private readonly ISysDepartmentService _sysDepartmentService;


        public UserDepartmentController(ILogger<UserDepartmentController> logger, TokenManager tokenManager, ISysUserDepartmentService sysUserDepartmentService,ISysUsersService sysUsersService,ISysDepartmentService sysDepartmentService)
        {
            _logger = logger;
            _tokenManager = tokenManager;
            _sysUserDepartmentService = sysUserDepartmentService;
            _userService = sysUsersService;
            _sysDepartmentService = sysDepartmentService;
        }


        /// <summary>
        /// 查询部门下用户id
        /// </summary>
        /// <param name="departmentId">部门id</param>
        /// <returns></returns>
        [HttpGet]
        [Authorization]
        public IActionResult GetUserDepartment(string departmentId)
        {
            if (string.IsNullOrEmpty(departmentId))
            {
                return toResponse(StatusCodeType.Error, "departmentId 不能为空");
            }
            var departids = _sysDepartmentService.GetWhere(m => m.PathID.Contains(departmentId)).Select(m => m.ID).ToList();

            return toResponse(_sysUserDepartmentService.GetDepartmentUser(departids));
        }

        /// <summary>
        /// 查询部门未添加用户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorization(Power = "PRIV_DEPARTMENT_VIEW")]
        public IActionResult GetExcludeUsers()
        {
           
            var response = _sysUserDepartmentService.GetNoinDepartmentUserList();

            //// 获取未添加用户
            //var response = _userService.GetWhere(m => !userIds.Contains(m.UserID)).OrderBy(m => m.CreateTime);

            return toResponse(response);
        }

        /// <summary>
        /// 更新部门用户
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorization(Power = "PRIV_DEPARTMENT_UPDATE")]
        public IActionResult UpdateUserDepartment([FromBody] UserDepartmentDto parm)
        {
            if (string.IsNullOrEmpty(parm.DepartmentId))
            {
                return toResponse(StatusCodeType.Error, "departmentId 不能为空");
            }

            //生成插入对象
            List<Sys_Userdepartment> rolePowers = new List<Sys_Userdepartment>();
            if (parm.InsertUserIds.Count() > 0)
            {
                foreach (var power in parm.InsertUserIds)
                {
                    rolePowers.Add(new Sys_Userdepartment
                    {
                        ID = GetGUID,
                        UserID = power,
                        DepartmentID = parm.DepartmentId
                    });
                }
                // 再插入传递进来的权限
                _sysUserDepartmentService.Add(rolePowers);
            }
            if(parm.RemoveUserIds.Count>0)
            {
                var response = _sysUserDepartmentService.Delete(m => parm.RemoveUserIds.Contains(m.UserID)
           );
            }          

            return toResponse(StatusCodeType.Success);

            
        }
    }
}
